package com.lavida.project.system.checkrule.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lavida.common.utils.security.ShiroUtils;
import com.lavida.framework.aspectj.lang.annotation.Log;
import com.lavida.framework.aspectj.lang.enums.BusinessType;
import com.lavida.framework.web.controller.BaseController;
import com.lavida.framework.web.page.TableDataInfo;
import com.lavida.project.system.checkrule.domain.CheckRule;
import com.lavida.project.system.checkrule.service.CheckRuleService;
import com.lavida.project.system.file.domain.Profiles;
import com.lavida.project.system.user.domain.User;
import com.lavida.project.system.user.service.IUserService;


@Controller
@RequestMapping("/system/checkrule")
public class CheckRuleConreoller extends BaseController{

	private String prefix = "system/checkrule";
	@Autowired
	private CheckRuleService checkRuleService;
	@Autowired
	private IUserService userService;
	@RequiresPermissions("wage:checkrule:view")
	@RequestMapping() 
	public String accu(ModelMap mmap) {
		User user  = ShiroUtils.getSysUser();
		User user1 = userService.selectUserById(user.getUserId());
		mmap.put("user", user1);
		return prefix+"/checkrule";
	}
	@RequiresPermissions("wage:checkrule:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(CheckRule checkRule) {		
		startPage();
		List<CheckRule> selectRule = checkRuleService.selectRule(checkRule);
		return getDataTable(selectRule);
	}
	@RequiresPermissions("wage:checkrule:add")
	@Log(title = "参数管理", businessType = BusinessType.INSERT)
	@RequestMapping("/showAdd")
	@ResponseBody
	public Map<String, Object> showAdd(CheckRule checkRule,HttpServletRequest request) {
		Map<String, Object> result = new HashMap<>();
		String msg = "";
		String status = "";
		String[] syString = request.getParameterValues("attenDate") ;
		  String attenDate = "";
		  if(syString!=null) {
	        for(int i = 0;i<syString.length;i++) {
	        	if(i!=syString.length-1) {
	        		attenDate=attenDate+syString[i]+",";
	            	System.out.println(attenDate.toString());
	        	}else {
	        		attenDate=attenDate+syString[i];
	        	}
	        }
		  }
	        checkRule.setAttenDate(attenDate);
	        try {
	        	checkRuleService.insertRule(checkRule);
	        	result.put("status","0");
	        }catch (Exception e) {
				// TODO: handle exception
	        	result.put("status", "20");
	        	result.put("msg", e);
			}
	        
		return result;
	}
	@RequiresPermissions("wage:checkrule:delete")
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@RequestMapping("/remove")
	@ResponseBody
	public Map<String, Object> remove(int id) {
		Map<String, Object> result = new HashMap<>();
		String status = "";
		String msg = "";
		try {
			int i = checkRuleService.deleteRule(id);
			status = "0";
		}catch (Exception e) {
			// TODO: handle exception
			status = "20";
			result.put("msg",e);
		}
		result.put("status", status);
		return result;
	}
	@RequiresPermissions("wage:checkrule:delete")
	@Log(title = "参数管理", businessType = BusinessType.DELETE)
	@RequestMapping("/removelist")
	@ResponseBody
	public Map<String, Object> removelist(int[] ids) {
		Map<String, Object> result = new HashMap<>();
		String status = "";
		int i = 0;
		for(int id:ids) {
			checkRuleService.deleteRule(id);
			i++;
		}
		if (i>=1) {
			status = "0";
		}else {
			status = "500";
		}
		result.put("status", status);
		return result;
	}
	@RequiresPermissions("wage:checkrule:list")
	@PostMapping("/getRule")
	@ResponseBody
	public Map<String, Object> getRule(int id){
		Map<String, Object> result = new HashMap<>();
		CheckRule checkRule = checkRuleService.getRule(id);
		result.put("checkRule", checkRule);
		return result;
	}
	@RequiresPermissions("wage:checkrule:update")
	@Log(title = "参数管理", businessType = BusinessType.UPDATE)
	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(CheckRule checkRule,HttpServletRequest request) {
		Map<String, Object> result = new HashMap<>();
		String[] syString = request.getParameterValues("attenDate") ;
	    String attenDate = "";
	    if(syString!=null) {
           for(int i = 0;i<syString.length;i++) {
        	  if(i!=syString.length-1) {
        		  attenDate=attenDate+syString[i]+",";
            	  System.out.println(attenDate.toString());
        	  }else {
        		  attenDate=attenDate+syString[i];
        	  }
           }
	    }
        checkRule.setAttenDate(attenDate);
        try {
        	checkRuleService.updateRule(checkRule);
        	result.put("status","0");
        }catch (Exception e) {
			// TODO: handle exception
        	result.put("status", "20");
        	result.put("msg", e);
		}
		return result;
	}
}
